﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevelopTool.Common;
using DevelopTool.Forms.LodingForm;
using DevelopTool.Model;
using Sunny.UI;

namespace DevelopTool.Forms.CommonHelpers
{
    public class MsgHelper
    {
        /// <summary>
        /// 将DataGridView选择的单元格内容推送消息
        /// </summary>
        /// <param name="dataGridView"></param>
        public static void SendMsg(DataGridView dataGridView)
        {
            if(string.IsNullOrEmpty(Setting.System.FeiShuMsgHookAddress))
            {
                UINotifierHelper.ShowNotifier($"请先配置飞书消息Hook地址",UINotifierType.WARNING,UIStyles.CurrentResources.InfoTitle,false,2000);
                return;
            }

            if(dataGridView == null) return;

            List<int> columnIndexList = new List<int>();
            List<int> rowIndexList = new List<int>();

            foreach(DataGridViewCell cell in dataGridView.SelectedCells)
            {
                if(!columnIndexList.Any(it => it == cell.ColumnIndex))
                    columnIndexList.Add(cell.ColumnIndex);

                if(!rowIndexList.Any(it => it == cell.RowIndex))
                    rowIndexList.Add(cell.RowIndex);
            }

            columnIndexList.Reverse();
            rowIndexList.Reverse();

            List<string> rowStrList=new List<string>();

            foreach(int rowIndex in rowIndexList)
            {
                List<string> columnStrList = new List<string>();

                foreach(int columnIndex in columnIndexList)
                {
                    columnStrList.Add(dataGridView.Rows[rowIndex].Cells[columnIndex].Value.ToString());
                }

                rowStrList.Add(string.Join("\\t",columnStrList));
            }


            string text = string.Empty;
            string contentType = "Text";
            if(dataGridView.SelectedCells.Count > 1)
            {
                text = string.Join("\\n",rowStrList);
            }
            else
            {
                if(dataGridView.SelectedCells[0].OwningColumn.Tag is DataGridColumn dataGridColumn)
                {
                    if(dataGridColumn.ShowFormProperty == null)
                        text = dataGridView.SelectedCells[0].Value.ToString();

                    else
                    {
                        text = dataGridView.SelectedCells[0].Value.ToString();
                        contentType = dataGridColumn.ShowFormProperty.TextType;
                    }
                }
                else
                {
                    text = string.Join("\\n",rowStrList);
                }
            }


            Tuple<bool,string> sendResult = new Tuple<bool,string>(false,"推送失败");
            LoadingHelper.ShowLoading("正在推送内容",() =>
            {
                sendResult = FeiShuMsgHelper.SendCardMsg(text,"内容推送",contentType: contentType);
            });

            if(sendResult.Item1)
            {
                UINotifierHelper.ShowNotifier("推送内容成功",UINotifierType.OK,UIStyles.CurrentResources.InfoTitle,false,2000);
            }
            else
            {
                UINotifierHelper.ShowNotifier(sendResult.Item2,UINotifierType.WARNING,UIStyles.CurrentResources.WarningTitle,false,2000);
            }
        }
    }
}
